package com.ljf.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljf.annotation.HasPermission;
import com.ljf.dto.StoreOutPageQuery;
import com.ljf.entity.DetailStoreGoods;
import com.ljf.service.DetailStoreGoodsService;
import com.ljf.util.Result;
import com.ljf.vo.DetailStoreGoodsOutVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotEmpty;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/inventory_management/detail_store_goods_out")
@Validated
public class StoreOutController {


    @Autowired
    private DetailStoreGoodsService detailStoreGoodsService;
    @GetMapping("/initOutOptions")
    public Result<Map<String, Object>> initOutOptions(){
        Map<String,Object> map = detailStoreGoodsService.initOutOptions();
        return Result.success(map);
    }

    @PostMapping("/queryPageByQoOut")
    public Result pageQuery(StoreOutPageQuery so){
        Page<DetailStoreGoodsOutVo> page = detailStoreGoodsService.pageQuery(so);
        return Result.success(page);
    }

    /**
     * 删除
     * @param cn
     * @return
     */
    @PostMapping("/delOut")
    public Result deleteOut(@NotEmpty(message = "系统编号不能为空")String cn){
        detailStoreGoodsService.deleteOut(cn);
        return Result.success();
    }


    @HasPermission("inventory_management:detail_store_goods_out:save")
    @PostMapping("/save")
    public Result saveOut(DetailStoreGoods detailStoreGoods, HttpServletRequest request){
        detailStoreGoodsService.saveOut(detailStoreGoods,(String) request.getHeader("token"));
        return Result.success();
    }


    /**
     * 商品出库
     * @param gid
     * @return
     */
    @HasPermission("inventory_management:detail_store_goods_out:save")
    @GetMapping("/changeOutGoods")
    public Result changeOutGoods(Long gid){
        return Result.success(detailStoreGoodsService.changeOutGoods(gid));
    }

    /**
     * 商品出库
     * @param storeId
     * @return
     */
    @HasPermission("inventory_management:detail_store_goods_out:save")
    @GetMapping("/changeOutStore")
    public Result changeOutStore(Long storeId){
        return Result.success(detailStoreGoodsService.changeOutStore(storeId));
    }

    @PostMapping("/queryOutGoods")
    public Result queryOutGoods(@RequestParam("goodsId") Long goodsId, @RequestParam("storeId") Long storeId){
        DetailStoreGoodsOutVo vo = detailStoreGoodsService.queryOutGoods(goodsId,storeId);
        return Result.success(vo);
    }
}
